package com.alibaba.wallstreet.uitls;

import java.util.List;

import javax.annotation.Resource;

import com.alibaba.nonda.utils.StringUtils;
import com.alibaba.rock.encrypt.IdUtil;
import com.alibaba.wallstreet.dal.mapper.GwsPoHeaderMapperExt;
import com.alibaba.wallstreet.dal.model.GwsPoHeader;
import com.alibaba.wallstreet.dal.model.GwsPoHeaderExample;

public class SystemConfig {

    // 采购系统url
    private String               ceresDomain;
    private String               rockyDomain;
    private String               fpmDomain;
    @Resource
    private IdUtil               idUtil;
    @Resource
    private GwsPoHeaderMapperExt gwsPoHeaderMapperExt;

    public String getPoDetailUrl(String poNo) {
        GwsPoHeader poHeader = getPoHeaderByPoNo(poNo);
        return getPoDetailUrl(poHeader);
    }

    public String getPoDetailUrl(GwsPoHeader poHeader) {
        if (poHeader == null) {
            return null;
        }
        if (isCeresSource(poHeader)) {
            return ceresDomain + "/ceres/po/poDetail.htm?operateCode=view&encPoNumber="
                   + idUtil.aesEncodeExternal(poHeader.getPoNo());
        } else if (isRockySource(poHeader)) {
            return rockyDomain + "/rocky/contract/search/structredContract.htm?contractId=&encryptContractId="
                   + idUtil.aesFixEncode(poHeader.getOutPoId());
        } else if (isFpmSource(poHeader)) {
            return fpmDomain + "/paycenter/fpm/OrderDetail.htm?headerId=" + poHeader.getOutPoId();
        }
        return null;
    }

    /**
     * @param poHeader
     * @return
     */
    private boolean isFpmSource(GwsPoHeader poHeader) {
        return "FPM".equals(poHeader.getSystemSource());
    }

    private boolean isRockySource(GwsPoHeader poHeader) {
        return "ROCKY".equals(poHeader.getSystemSource());
    }

    public String getPrDetailUrl(String prNo) {
        return ceresDomain + "/ceres/pr/requisitionDetail.htm?operateCode=view&reqNumber="
               + idUtil.aesEncodeExternal(prNo);
    }

    public String getContractDetailUrl(String contractNo) {
        return ceresDomain + "/ceres/contract/viewSearchContractInfo.htm?contractCodeEncode="
               + idUtil.aesEncodeExternal(contractNo);
    }

    public String getRcvDetailUrl(String outRcvId) {
        return ceresDomain + "/ceres/po/rcvDetail.htm?operateCode=view&idCode=" + idUtil.aesEncodeExternal(outRcvId);
    }

    public String getRtnDetailUrl(String outRcvId) {
        return ceresDomain + "/ceres/po/returnDetail.htm?operateCode=view&idCode=" + idUtil.aesEncodeExternal(outRcvId);
    }

    // 接收链接 param:poNo
    public String getRcvPoListUtl(String poNo) {
        return ceresDomain + "/ceres/supplier/ceresManagerIndex.htm#src=/ceres/po/rcvPoList.htm?poNo="
               + idUtil.aesEncodeExternal(poNo);
    }

    public String getPreNoticeDetailUrl(String pnNo, String poNo) {
        return "/paycenter/pay/paymentNotice/preNoticeDetail.htm?pnNoEncode=" + idUtil.aesEncodeExternal(pnNo)
               + "&poNoEncode=" + idUtil.aesEncodeExternal(poNo);
    }

    public String getPostNoticeDetailUrl(String pnNo, String poNo) {
        return "/paycenter/pay/paymentNotice/postNoticeDetail.htm?pnNoEncode=" + idUtil.aesEncodeExternal(pnNo)
               + "&poNoEncode=" + idUtil.aesEncodeExternal(poNo);
    }

    public String getApproveManagerUrl(String paNoEncode) {
        return "/paycenter/pay/payapply/approveManager.htm?applyNoEncode=" + idUtil.aesEncodeExternal(paNoEncode);
    }

    // 获取预提确认（审批）页面URL
    public String getAccruedConfirmUrl(Long accruedId) {
        return "https://wallstreet.alibaba-inc.com/accrued/order/detail.htm?id=" + accruedId;
    }

    public Long decode(String idCode) {
        return idUtil.aesFixDecode(idCode);
    }

    public String getCeresDomain() {
        return ceresDomain;
    }

    public void setCeresDomain(String ceresDomain) {
        this.ceresDomain = ceresDomain;
    }

    // private boolean isCeresSource(String poNo) {
    // GwsPoHeader poHeader = getPoHeaderByPoNo(poNo);
    // return isCeresSource(poHeader);
    // }

    private boolean isCeresSource(GwsPoHeader poHeader) {
        if (poHeader != null && "CERES".equals(poHeader.getSystemSource())) {
            return true;
        }
        return false;
    }

    private GwsPoHeader getPoHeaderByPoNo(String poNo) {
        if (StringUtils.isBlank(poNo)) return null;
        GwsPoHeaderExample example = new GwsPoHeaderExample();
        example.createCriteria().andPoNoEqualTo(poNo);
        List<GwsPoHeader> list = gwsPoHeaderMapperExt.selectByExample(example);

        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public String getRockyDomain() {
        return rockyDomain;
    }

    public void setRockyDomain(String rockyDomain) {
        this.rockyDomain = rockyDomain;
    }

    public String getFpmDomain() {
        return fpmDomain;
    }

    public void setFpmDomain(String fpmDomain) {
        this.fpmDomain = fpmDomain;
    }

}
